const app = getApp()
const request = require('../../utils/request.js')

Page({
  data: {
    myMoney: 0,
    needMoney: 0,
    diffMoney: 0,
    isCanWithdraw: false,
    isWithdrawDisable: true,
    inputMoney: 0,
    isWithdrawVisible: 'hidden',
    withdrawTips: ''
  },

  moneyLog: function() {
    wx.navigateTo({
      url: '../moneyLog/moneyLog',
    })
  },

  onLoad: function (option){
    this.setData({
      myMoney: option.myMoney,
      needMoney: option.needMoney,
      diffMoney: (option.needMoney - option.myMoney).toFixed(3)
    });
    console.log('lcsssssssssssssssssss', option.myMoney);
    console.log('lcsssssssssssssssssss', option.needMoney);
  },

  onShow: function () {
    wx.setNavigationBarTitle({
      title: '提现',
    });

    this.getMoney(this.data.needMoney - this.data.myMoney);
  },

  // share
  onShareAppMessage: function(res) {
    if (res.from === 'button') {
      let url = "/pages/index/index?from_open_id=" + this.data.myOpenId
      let title = app.globalData.userInfo.nickName + "@你，【运动步数能赚钱】你还不知道吗？"
      return {
        title: title,
        imageUrl: "/images/publicize_3.jpg",
        path: url,
      }
    }
  },

  //bindKeyInput
  bindKeyInput: function(e) {
    console.log('mmmmmmm', e.detail.value);
    if (e.detail.value > 0) {
      this.setData({
        isWithdrawDisable: !(this.data.isCanWithdraw && true),
        inputMoney: e.detail.value
      })
    } else {
      this.setData({
        isWithdrawDisable: !(this.data.isCanWithdraw && false),
        inputMoney: e.detail.value
      })
    }
  },

  // btnIsClick
  btnIsClick: function() {
    if (this.data.inputMoney == 0) {
      this.setData({
        isWithdrawVisible: 'visible'
      });
    } else {
      this.withdrawMoney();
    }
  },


  //withdrawMoney
  withdrawMoney: function() {
    console.log('开始提现')
    request.withdraw(this.data.inputMoney).then(res => {
      console.log('提现结果是dlkfjdljfjdklfjl：', res.data.code);
      if (res.data.code == 1) {
        // 提现失败
        this.showWithdrawFail(res.data.msg);
      } else {
        wx.showModal({
          content: '提现申请已成功提交，将在24小时内转入你的账户中，请注意查收！',
          showCancel: false,
          confirmText: '知道了'
        })

        // 把输入框的input 金额改为0
        let diffMoney = (res.data.need_money / 10000 - res.data.money / 10000).toFixed(3);
        this.getMoney(diffMoney);
        this.setData({
          isWithdrawDisable: true,
          myMoney: (res.data.money / 10000).toFixed(3),
          needMoney: res.data.need_money / 10000,
          diffMoney: diffMoney
        })
      }
    });
  },

  // 提现失败的提示框
  showWithdrawFail: function(msg){
    wx.showToast({
      title: msg,
      icon: "none",
      duration: 1500
    })
  },

  getMoney: function (diffMoney){
      // let money = 100;
    if (diffMoney > 0) {
        this.setData({
          isCanWithdraw: false,
          isWithdrawDisable: true,
        })
    }else{
        this.setData({
          isCanWithdraw: true,
          isWithdrawDisable: false
        })
    }
  },
})